package com.dtruth.dataflow.util;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.THttpClient;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;

import com.feheadline.cms.general.etl.service.thrift.gen.ETLThriftService;

public class ETLServiceClient {
	private ETLThriftService.Client client;
	private TTransport transport;

	/**
	 * get thrift service client
	 * 
	 * @return client
	 */
	public synchronized ETLThriftService.Client getClient(String addr) {
		if (transport == null || !transport.isOpen()) {
			try {
				transport = new THttpClient(addr);
				TProtocol protocol = new TBinaryProtocol(transport);
				client = new ETLThriftService.Client(protocol);
				transport.open();
			} catch (TTransportException e) {
				e.printStackTrace();
			}
		}
		return client;
	}

	/**
	 * close thrift service client
	 */
	public synchronized void closeClient() {
		if (transport.isOpen())
			transport.close();

	}
}
